// src\views\sendbox\audit-manage\AuditList.jsx
// 审核列表
import { Table, Tag, Button, notification } from 'antd'
import axios from 'axios';
import React, { useEffect, useState } from 'react'

export default function AuditList(props) {
  const [dataSource, setDataSource] = useState([]);
  const columns = [
    {
      title: "新闻列表",
      dataIndex: 'title',
      key: "title",
      render: (title, item) => {
        return <Button type="link" onClick={() => { props.history.push(`/news-manage/preview/${item.id}`) }}>{title}</Button>
      }
    },
    {
      title: "作者",
      dataIndex: 'author',
      key: "author"
    },
    {
      title: "新闻分类",
      dataIndex: 'category',
      key: "category",
      render: (category) => {
        return <div>{category.title}</div>
      }
    },
    {
      title: "审核状态",
      dataIndex: 'auditState',
      key: "auditState",
      render: (auditState) => {
        const colorList = ["", 'orange', 'green', 'red']
        const auditList = ["草稿箱", "审核中", "已通过", "未通过"]
        return <Tag color={colorList[auditState]}>{auditList[auditState]}</Tag>
      }
    },
    {
      title: "操作",
      render: (_, item) => {
        return (
          <div>
            {
              // 审核中
              item.auditState === 1 && <Button onClick={() => handleRervert(item)} >撤销</Button>
            }
            {
              //已通过
              item.auditState === 2 && <Button danger onClick={() => handlePublish(item)}>发布</Button>
            }
            {
              // 未通过
              item.auditState === 3 && <Button type="primary" onClick={() => handleUpdate(item)}>打回修改</Button>
            }
          </div>
        )
      }
    },
  ];
  // 打回修改
  const handleUpdate = (item) => {
    props.history.push(`/news-manage/update/${item.id}`);
  }
  // 发布
  const handlePublish = (item) => {
    axios.patch(`http://localhost:8888/news/${item.id}`, {
      publishState: 2,
      publishTime: Date.now()
    }).then(res => {
      props.history.push('/publish-manage/published');
      notification.info({
        message: `通知`,
        description:
          `您可以到【发布管理/已经发布】中查看您的新闻`,
        placement: "bottomRight"
      });
    })
  }
  //取消审核
  const handleRervert = (item) => {
    setDataSource(dataSource.filter(e => e.id !== item.id));
    axios.patch(`http://localhost:8888/news/${item.id}`, {
      auditState: 0
    }).then(res => {
      notification.info({
        message: `通知`,
        description:
          `您可以到草稿箱中查看您的新闻`,
        placement: "bottomRight"
      });
    })
  }
  const { username } = JSON.parse(localStorage.getItem('token'));
  useEffect(() => {
    // _ne=0表示小于0
    // _lte=1表示大于1
    axios.get(`http://localhost:8888/news?author=admin&auditState_ne=0&publishState_lte=1&_expand=category`).then(res => {
      setDataSource(res.data);
    })
  }, [username]);
  return (
    <div>
      <Table dataSource={dataSource} columns={columns}
        pagination={{
          pageSize: 5
        }}
        rowKey={item => item.id}
      ></Table>
    </div>
  )
}
